package com.ydh.company.service.impl;

import com.ydh.company.dto.ResponseData;
import com.ydh.company.dto.ResultData;
import com.ydh.company.mapper.PurchaseReturnGoodsMapper;
import com.ydh.company.mapper.WarehousingOutRecordMapper;
import com.ydh.company.pojo.PurchaseReturnGoodsDetail;
import com.ydh.company.pojo.PurchaseReturnGoodsMain;
import com.ydh.company.pojo.WarehousingOutRecordDetail;
import com.ydh.company.pojo.WarehousingOutRecordMain;
import com.ydh.company.service.PurchaseReturnGoodsService;
import com.ydh.company.utils.GetOddNumbersUtil;
import com.ydh.company.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author YuYinYang
 * @date Creat at 2020/5/7
 * @description:
 **/
@Service
@SuppressWarnings("all")
public class PurchaseReturnGoodsServiceImpl implements PurchaseReturnGoodsService {
@Autowired
private PurchaseReturnGoodsMapper  purchaseReturnGoodsMapper;
@Autowired
private WarehousingOutRecordMapper warehousingOutRecordMapper;
    @Override
    public ResultData addReturnGoodsOrder(PurchaseReturnGoodsMain purchaseReturnGoodsMain) {
       int res1=0;
       int res2=0;
       purchaseReturnGoodsMain.setPurchaseReturnGoodsMainId(UUID.randomUUID().toString());
       res2=purchaseReturnGoodsMapper.addReturnGoodsOrderMain(purchaseReturnGoodsMain);
        for (PurchaseReturnGoodsDetail p:purchaseReturnGoodsMain.getPurchaseReturnGoodsDetail()
             ) {
            p.setPurchaseOrderDetailId(UUID.randomUUID().toString());
            res2=purchaseReturnGoodsMapper.addReturnGoodsOrderDetail(p);
        }
        if (res1>0&res2>0){
              //添加退货单信息后要添加出库信息
            int res3=0;
            int res4=0;
            WarehousingOutRecordMain warehousingOutRecordMain= new WarehousingOutRecordMain();
            warehousingOutRecordMain.setWarehousingRecordMainId(UUIDUtil.getId());
            warehousingOutRecordMain.setReceiptOutNo(GetOddNumbersUtil.getOddNumbers());
            warehousingOutRecordMain.setDate(purchaseReturnGoodsMain.getDate());
            warehousingOutRecordMain.setRepositoryId(purchaseReturnGoodsMain.getRepositoryId());
            warehousingOutRecordMain.setCreator(purchaseReturnGoodsMain.getCreator());
            warehousingOutRecordMain.setHandledBy(purchaseReturnGoodsMain.getHandledBy());
            warehousingOutRecordMain.setStockInOutType("采购退货");
            warehousingOutRecordMain.setRecordType(0);
            warehousingOutRecordMain.setTotalCount(purchaseReturnGoodsMain.getTotalcCount());
            warehousingOutRecordMapper.addWarehousingRecordMain(warehousingOutRecordMain);
            for (PurchaseReturnGoodsDetail p:purchaseReturnGoodsMain.getPurchaseReturnGoodsDetail()
                 ) {
                WarehousingOutRecordDetail warehousingOutRecordDetail = new WarehousingOutRecordDetail();
                warehousingOutRecordDetail.setWarehousingRecordDetailId(UUID.randomUUID().toString());
                warehousingOutRecordDetail.setWarehousingRecordMainId(warehousingOutRecordMain.getWarehousingRecordMainId());
                warehousingOutRecordDetail.setStockInOut(p.getReturnGoodsNumber());
                warehousingOutRecordDetail.setGoodsId(p.getGoodsId());
               warehousingOutRecordDetail.setReceiptOutNo(warehousingOutRecordMain.getReceiptOutNo());
            warehousingOutRecordMapper.addWarehousingRecordDetail(warehousingOutRecordDetail);
            }
            return ResponseData.success("成功增加");

        }
        return ResponseData.error(4001,"增加失败");
    }
}
